package com.press.handlers;

import com.press.controllers.InitController;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by haocongshun on 2016/10/1.
 */
public class UrlHandler extends UrlHandlerAbs {
    private static Logger log = Logger.getLogger(UrlHandler.class);
    public void dohandle(String url, HttpServletRequest request, HttpServletResponse response, Boolean[] isDohandle) throws ServletException, IOException {
        String ctxPath = request.getContextPath();
        String forwordPath = "";
        isDohandle[0]=true;
        String newUrl = url.substring(ctxPath.length());
        switch (newUrl){
            case "/admin":
                forwordPath="/WEB-INF/admin/login.jsp";
                break;
            case "/":
                forwordPath="/blog/index.press";
                break;
            case "/index":
                forwordPath="/blog/index.press";
                break;
            case "/content":
                forwordPath="/blog/content.press";
                break;
            case "/taxonomy":
                forwordPath="/blog/taxonomy.press";
                break;
            case "/user":
                forwordPath="/blog/user.press";
                break;
            case "/search":
                forwordPath="/blog/search.press";
                break;
            default:
                if(newUrl.startsWith("/admin")){
                    log.info("/WEB-INF"+newUrl);
                    request.getRequestDispatcher("/WEB-INF"+newUrl).forward(request,response);
                    isDohandle[0]=true;
                    return;
                }
                isDohandle[0]=false;
                break;
        }
        if(isDohandle[0]){
            request.getRequestDispatcher(forwordPath).forward(request,response);
        }
    }
}
